package com.ld.admin.utils;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;
/**
 * @ClassName ExcelStyleUtil
 * @Description 样式封装类
 * @Author linhao
 * @Date 2019/8/13 9:06
 * @ModifyDate 2019/8/13 9:06
 * @Version 1.0
 */
public class ExcelStyleUtil implements IExcelExportStyler {

    /**
     * 获取与给定格式字符串匹配的格式索引
     */
    private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
    private static final short FONT_SIZE_TEN = 10;
    private static final short FONT_SIZE_ELEVEN = 11;
    private static final short FONT_SIZE_TWELVE = 12;
    /**
     * 大标题样式
     */
    private CellStyle headerStyle;
    /**
     * 每列标题样式
     */
    private CellStyle titleStyle;
    /**
     * 数据行样式
     */
    private CellStyle styles;

    public ExcelStyleUtil(Workbook workbook) {
        this.init(workbook);
    }

    /**
     * @Description 初始化样式
     * @Author linhao
     * @Date 2019/8/20 11:11
     * @ModifyDate 2019/8/20 11:11
     * @Params [workbook：Excel工作簿的高级表示形式类]
     * @Return void
     */
    private void init(Workbook workbook) {
        this.headerStyle = initHeaderStyle(workbook);
        this.titleStyle = initTitleStyle(workbook);
        this.styles = initStyles(workbook);
    }

    /**
     * @Description 大标题样式
     * @Author linhao
     * @Date 2019/8/20 11:12
     * @ModifyDate 2019/8/20 11:12
     * @Params [color：short类型的color]
     * @Return org.apache.poi.ss.usermodel.CellStyle：返回大标题样式变量
     */
    @Override
    public CellStyle getHeaderStyle(short color) {
        //这里添加大标题样式的样式
        return headerStyle;
    }

    /**
     * @Description 每列标题样式
     * @Author linhao
     * @Date 2019/8/20 11:15
     * @ModifyDate 2019/8/20 11:15
     * @Params [color:short类型的color]
     * @Return org.apache.poi.ss.usermodel.CellStyle:返回每列标题样式变量
     */
    @Override
    public CellStyle getTitleStyle(short color) {
        //这里添加每列标题样式的样式
        return titleStyle;
    }

    /**
     * @Description 数据行样式
     * @Author linhao
     * @Date 2019/8/20 11:16
     * @ModifyDate 2019/8/20 11:16
     * @Params [parity:可以用来表示奇偶行, entity：数据内容]
     * @Return org.apache.poi.ss.usermodel.CellStyle：样式
     */
    @Override
    public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
        //这里添加数据行样式的样式
        return styles;
    }

    /**
     * @Description 获取样式方法
     * @Author linhao
     * @Date 2019/8/20 11:17
     * @ModifyDate 2019/8/20 11:17
     * @Params [cell:数据行, dataRow：每行数据, entity, obj：对象, data:数据]
     * @Return org.apache.poi.ss.usermodel.CellStyle：样式
     */
    @Override
    public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
        return getStyles(true, entity);
    }

    /**
     * @Description 模板使用的样式设置
     * @Author linhao
     * @Date 2019/8/20 11:19
     * @ModifyDate 2019/8/20 11:19
     * @Params [isSingle:是否是单个, excelForEachParams：excel的数据集合]
     * @Return org.apache.poi.ss.usermodel.CellStyle：样式
     */
    @Override
    public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
        return null;
    }

    /**
     * @Description 初始化--大标题样式
     * @Author linhao
     * @Date 2019/8/20 11:21
     * @ModifyDate 2019/8/20 11:21
     * @Params [workbook：Excel工作簿的高级表示形式类]
     * @Return org.apache.poi.ss.usermodel.CellStyle：样式
     */
    private CellStyle initHeaderStyle(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);//调用基础样式方法获取样式
        //样式设置字体
        style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
        return style;
    }

    /**
     * @Description 初始化--每列标题样式
     * @Author linhao
     * @Date 2019/8/20 11:22
     * @ModifyDate 2019/8/20 11:22
     * @Params [workbook：Excel工作簿的高级表示形式类]
     * @Return org.apache.poi.ss.usermodel.CellStyle:样式
     */
    private CellStyle initTitleStyle(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);//调用基础样式方法获取样式
        style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));        //样式设置字体
        //字体背景色
        //style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        //style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return style;
    }

    /**
     * @Description 初始化--数据行样式
     * @Author linhao
     * @Date 2019/8/20 11:24
     * @ModifyDate 2019/8/20 11:24
     * @Params [workbook：Excel工作簿的高级表示形式类]
     * @Return org.apache.poi.ss.usermodel.CellStyle:样式
     */
    private CellStyle initStyles(Workbook workbook) {
        CellStyle style = getBaseCellStyle(workbook);//调用基础样式方法获取样式
        //设置4个边框的宽度
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
        style.setDataFormat(STRING_FORMAT);
        return style;
    }

    /**
     * @Description 基础样式 类的核心
     * @Author linhao
     * @Date 2019/8/20 11:25
     * @ModifyDate 2019/8/20 11:25
     * @Params [workbook：Excel工作簿的高级表示形式类]
     * @Return org.apache.poi.ss.usermodel.CellStyle:样式
     */
    private CellStyle getBaseCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        //下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);
        //水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);
        return style;
    }

    /**
     * @Description 字体样式
     * @Author linhao
     * @Date 2019/8/20 11:26
     * @ModifyDate 2019/8/20 11:26
     * @Params [workbook:Excel工作簿的高级表示形式类, size:字体大小, isBold:是否加粗]
     * @Return org.apache.poi.ss.usermodel.Font :字体样式
     */
    private Font getFont(Workbook workbook, short size, boolean isBold) {
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("宋体");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
    }
}
